/**
 * 详情对话框
 */
var PositionInfoDlg = {
    data: {
        fId: "",
        fName: "",
        fContent: "",
        fTag: "",
        remark: "",
        fKeyWords: "",
    }
};

layui.use(['form', 'admin', 'ax', 'upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var upload = layui.upload;

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/fileRun/detail?fId=" + Feng.getUrlParam("fId"));
    var result = ajax.start();
    form.val('positionForm', result.data);

    //实例化编辑器
    var ue = UE.getEditor('container', {
        enableAutoSave: false,
        autoHeightEnabled: true,
        autoFloatEnabled: false,
        scaleEnabled: true,         //滚动条
        initialFrameHeight: 400     //默认的编辑区域高度
    });

    UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
    UE.Editor.prototype.getActionUrl = function (action) {
        if (action === 'uploadimage' || action === 'uploadscrawl' || action === 'uploadimage') {
            return Feng.ctxPath + '/fileRun/imgUpdate';
        } else if (action === 'uploadfile') {
            return Feng.ctxPath + '/ueditor/uploadfile';
        } else if (action === 'uploadvideo') {
            return Feng.ctxPath + '/ueditor/uploadvideo';
        } else {
            return this._bkGetActionUrl.call(this, action);
        }
    };

    //项目整体分类
    var categoryHtml ='';
    $.ajax({
        url: Feng.ctxPath + '/fileTag/getTaglist/1',
        type: 'get',
        dataType: 'json',
        success: function (res) {
            var roleList = res.records;
            console.log(roleList)
            let selectDate = result.data.fCategory
            //遍历所有的拿到的下拉数据
            for (i = 0; i < roleList.length; i++) {
                var istrue = false;
                if(roleList[i].id==selectDate){
                    istrue=true;
                }
                if(istrue){
                    categoryHtml += ' <option selected value="'+ roleList[i].id +'" >' + roleList[i].tagName + '</option>'
                }else{
                    categoryHtml += ' <option value="'+ roleList[i].id +'">' + roleList[i].tagName + '</option>'
                }
            }
            $("#category").append(categoryHtml);
            form.render();
        },
        error: function (e) {
            layer.msg('连接超时，请稍后再试');
        }
    });

    //技术栈标签
    var roleHtml = '';
    $.ajax({
        url: Feng.ctxPath + '/fileTag/getTaglist/2',
        type: 'get',
        dataType: 'json',
        success: function (res) {
            var roleList = res.records;
            console.log(roleList)
            //遍历所有的拿到的复选框数据
            let roleIdlist = result.data.fTag.split(",")
            for (i = 0; i < roleList.length; i++) {
                var istrue = false;
                for (y = 0; y < roleIdlist.length; y++) {
                    //双层for循环来匹配我们所要选中的那一项
                    if (roleIdlist[y] == roleList[i].id) {
                        istrue = true
                    }
                    // console.log(i)
                }
                if (istrue) {
                    //如果有就渲染选中的模板
                    roleHtml += ' <input type="checkbox" checked name="tag" lay-filter="like" title="' + roleList[i].tagName + '" value="' + roleList[i].id + '" lay-skin="primary">'
                } else {
                    //如果没有就渲染没有选中的模板
                    roleHtml += ' <input type="checkbox" name="tag" lay-filter="like" title="' + roleList[i].tagName + '" value="' + roleList[i].id + '" lay-skin="primary">'
                }
            }
            //初始化数据
            $("#taglist").append(roleHtml);
            form.render();
        },
        error: function (e) {
            layer.msg('连接超时，请稍后再试');
        }
    });



    //执行实例
    var uploadInst = upload.render({
        elem: '#fileBtn' //绑定元素
        , url: Feng.ctxPath + '/fileRun/upload' //上传接口
        , accept: 'file'
        , done: function (res) {
            $("#fUrl").val(res.data.fileUrl);
            Feng.success(res.message);
        }
        , error: function () {
            Feng.error("上传文件失败！");
        }
    });

    // //多图片上传
    // var imagesUrl='';
    // var uploadsInst = upload.render({
    //
    //     elem: '#filesBtn'
    //     ,url: Feng.ctxPath + '/fileRun/uploads'
    //     ,multiple: true
    //     ,before: function(obj){
    //         obj.preview(function(index, file, result){
    //             $('#demo2').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img" width="92px" height="92px" style="margin-right: 5px">')
    //         });
    //     }
    //     ,done: function(res){
    //         var loading = layer.load(0, {
    //             shade: [0.1,'#fff'],
    //             time: 5*1000
    //         });
    //         //上传完毕
    //         if(res.code===0){
    //             layer.close(loading);
    //             imagesUrl+=res.data.images
    //             $("#imagesUrl").val(imagesUrl);
    //         }else{
    //             layer.close(loading);
    //             Feng.error("上传文件失败！");
    //         }
    //     }
    //     , error: function () {
    //         // Feng.error("上传文件失败！");
    //     }
    // });

    var tagStr = '';
    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        $('input[type=checkbox]:checked').each(function () {
            tagStr += $(this).val() + ","
        });
        //数组
        console.log(tagStr);
        data.field.fTag = tagStr;
        var category = $('select[name="category"] option:selected').val();
        console.log(category);
        data.field.fCategory = category;
        var ajax = new $ax(Feng.ctxPath + "/fileRun/update", function (data) {
            if (data.code == 200) {
                Feng.success("更新成功！");
                //传给上个页面，刷新table用
                admin.putTempData('formOk', true);
                //关掉对话框
                admin.closeThisDialog();
            } else {
                Feng.error("更新失败！" + data.message)
            }
        });
        ajax.set(data.field);
        ajax.start();
        return false;
    });
});